package com.zheng.umbrella.web.controller.manage;

import com.zheng.common.base.BaseController;
import com.zheng.common.constant.Constant;
import com.zheng.common.util.*;
import com.zheng.umbrella.common.constant.UmbrellaConstant;
import com.zheng.umbrella.common.constant.UmbrellaResult;
import com.zheng.umbrella.common.constant.UmbrellaResultConstant;
import com.zheng.umbrella.dao.model.*;
import com.zheng.umbrella.rpc.api.UmbrellaHardwareMachineService;
import com.zheng.umbrella.rpc.api.UmbrellaHardwarePathService;
import com.zheng.umbrella.rpc.api.UmbrellaHardwareProductSerialnumberService;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.rpc.api.UpmsApiService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.*;
import java.util.zip.ZipOutputStream;

/**
 * Created by Administrator on 2018/9/16.
 */
@Controller
@Api(value = "出厂编号管理", description = "出厂编号管理")
@RequestMapping("/manage/serialnum")
public class UmbrellaHardwareSerialnumberController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UmbrellaHardwareSerialnumberController.class);

    @Autowired
    private UmbrellaHardwareProductSerialnumberService umbrellaHardwareProductSerialnumberService;
    @Autowired
    private UmbrellaHardwareMachineService umbrellaHardwareMachineService;
    @Autowired
    private UmbrellaHardwarePathService umbrellaHardwarePathService;
    @Autowired
    private UpmsApiService upmsApiService;


    @ApiOperation(value = "出厂编号首页")
    @RequiresPermissions("umbrella:serialnum:read")
    @RequestMapping(value = "/index", method = {RequestMethod.GET, RequestMethod.POST})
    public String index() {
        return "/manage/serialnum/index.jsp";
    }

    @ApiOperation(value = "出厂编号列表")
    @RequiresPermissions("umbrella:serialnum:read")
    @RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object list(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search,
            @RequestParam(required = false, value = "sort") String sort,
            @RequestParam(required = false, value = "order") String order,
            @RequestParam(required = false, defaultValue = "", value = "begintime") String begintime,
            @RequestParam(required = false, defaultValue = "", value = "endtime") String endtime,
            @RequestParam(required = false, value = "years") String years,
            @RequestParam(required = false, value = "classcode") String classcode,
            @RequestParam(required = false, value = "version") String version,
            @RequestParam(required = false, value = "subversion") String subversion,
            @RequestParam(required = false, value = "status") String status,
            @RequestParam(required = false, value = "direction") String direction,
            @RequestParam(required = false, value = "machineCode") String machineCode
            ) {
        //String userName = getLoginUserName();
        //Integer agentId = upmsApiService.selectUserAgentIdByUsername(userName);
        UmbrellaHardwareProductSerialnumberExample umbrellaHardwareProductSerialnumberExample = new UmbrellaHardwareProductSerialnumberExample();
        UmbrellaHardwareProductSerialnumberExample.Criteria criteria = umbrellaHardwareProductSerialnumberExample.createCriteria();

        if (StringUtils.isNotBlank(begintime) && StringUtils.isNotBlank(endtime)) {
            criteria.andCreatetimeBetween(DateUtil.parseDate(begintime,DateUtil.FORMAT_YYYY_MM_DD),DateUtil.parseDate(endtime,DateUtil.FORMAT_YYYY_MM_DD));
        }
        if (years != null) {
            criteria.andYearsEqualTo(years);
        }
        if (classcode != null) {
            criteria.andClasscodeEqualTo(classcode);
        }
        if (version != null) {
            criteria.andVersionEqualTo(version);
        }
        if (subversion != null) {
            criteria.andSubversionEqualTo(subversion);
        }
        if (status != null) {
            criteria.andStatusEqualTo(Integer.valueOf(status));
        }
        if (direction != null) {
            criteria.andDirectionEqualTo(Integer.valueOf(direction));
        }
        if (machineCode != null) {
            criteria.andMachineCodeEqualTo(machineCode);
        }
        if (StringUtils.isNotBlank(sort) && StringUtils.isNotBlank(order)) {
            umbrellaHardwareProductSerialnumberExample.setOrderByClause(sort + " " + order);
        }
        List<UmbrellaHardwareProductSerialnumber> rows = umbrellaHardwareProductSerialnumberService.selectByExampleForOffsetPage(umbrellaHardwareProductSerialnumberExample, offset, limit);
        long total = umbrellaHardwareProductSerialnumberService.countByExample(umbrellaHardwareProductSerialnumberExample);
        Map<String, Object> result = new HashMap<>();
        result.put("rows", rows);
        result.put("total", total);
        return result;
    }

    @ApiOperation(value = "新增出厂编号(按规则)")
    @RequiresPermissions("umbrella:serialnum:create")
    @RequestMapping(value = "/create", method = {RequestMethod.GET})
    public String create(ModelMap modelMap) {
        //状态
        List<UpmsDict> statusList = upmsApiService.selectUpmsDictByCode(Constant.UPMS_YES_NO);
        modelMap.put("statusList", statusList);
        //代号
        List<UpmsDict> machineTypeList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_MACHINE_TYPE);
        modelMap.put("machineTypeList", machineTypeList);
        //产品分类代号
        List<UpmsDict> classCodeList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_CLASSCODE);
        modelMap.put("classCodeList", classCodeList);
        //换代或变型代号
        List<UpmsDict> subVersionList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_MSUBVERSION);
        modelMap.put("subVersionList", subVersionList);
        //使用去向
        List<UpmsDict> directionList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_MDIRECTION);
        modelMap.put("directionList", directionList);

        Date nowDate = new Date();

        UmbrellaHardwareProductSerialnumber umbrellaHardwareProductSerialnumber = new UmbrellaHardwareProductSerialnumber();
        umbrellaHardwareProductSerialnumber.setYears(DateUtil.date2Str(nowDate,DateUtil.FORMAT_YY));
        umbrellaHardwareProductSerialnumber.setMonths(DateUtil.date2Str(nowDate,DateUtil.FORMAT_MM));

        modelMap.put("umbrellaHardwareProductSerialnumber",umbrellaHardwareProductSerialnumber);
        return "/manage/serialnum/create.jsp";
    }

    @ApiOperation(value = "新增出厂编号(按规则)")
    @RequiresPermissions("umbrella:serialnum:create")
    @ResponseBody
    @RequestMapping(value = "/create", method = {RequestMethod.POST})
    public Object create(@RequestParam(required = true, value = "batchCount") Integer batchCount,UmbrellaHardwareProductSerialnumber umbrellaHardwareProductSerialnumber) {
        String userName = getLoginUserName();
        Date nowDate = new Date();
        umbrellaHardwareProductSerialnumber.setYears(StringUtils.leftPad(umbrellaHardwareProductSerialnumber.getYears(),2,"0"));
        umbrellaHardwareProductSerialnumber.setSubversion(StringUtils.leftPad(umbrellaHardwareProductSerialnumber.getSubversion(),2,"0"));
        umbrellaHardwareProductSerialnumber.setMonths(StringUtils.leftPad(umbrellaHardwareProductSerialnumber.getMonths(),2,"0"));
        umbrellaHardwareProductSerialnumber.setBatchnumber(StringUtils.leftPad(umbrellaHardwareProductSerialnumber.getBatchnumber(),3,"0"));

        umbrellaHardwareProductSerialnumber.setCreateuser(userName);
        umbrellaHardwareProductSerialnumber.setCreatetime(nowDate);
        umbrellaHardwareProductSerialnumber.setUpdateuser(userName);
        umbrellaHardwareProductSerialnumber.setUpdatetime(nowDate);

        //查询是否存在此批号及计算此批号最大剩余数量
        UmbrellaHardwareProductSerialnumberExample umbrellaHardwareProductSerialnumberExample = new UmbrellaHardwareProductSerialnumberExample();
        UmbrellaHardwareProductSerialnumberExample.Criteria criteria = umbrellaHardwareProductSerialnumberExample.createCriteria();
        if (StringUtils.isNotBlank(umbrellaHardwareProductSerialnumber.getYears())) {
            criteria.andYearsEqualTo(umbrellaHardwareProductSerialnumber.getYears());
        }
        if (StringUtils.isNotBlank(umbrellaHardwareProductSerialnumber.getClasscode())) {
            criteria.andClasscodeEqualTo(umbrellaHardwareProductSerialnumber.getClasscode());
        }
        if (StringUtils.isNotBlank(umbrellaHardwareProductSerialnumber.getVersion())) {
            criteria.andVersionEqualTo(umbrellaHardwareProductSerialnumber.getVersion());
        }
        if (StringUtils.isNotBlank(umbrellaHardwareProductSerialnumber.getSubversion())) {
            criteria.andSubversionEqualTo(umbrellaHardwareProductSerialnumber.getSubversion());
        }
        if (StringUtils.isNotBlank(umbrellaHardwareProductSerialnumber.getMonths())) {
            criteria.andMonthsEqualTo(umbrellaHardwareProductSerialnumber.getMonths());
        }
        if (StringUtils.isNotBlank(umbrellaHardwareProductSerialnumber.getBatchnumber())) {
            criteria.andBatchnumberEqualTo(umbrellaHardwareProductSerialnumber.getBatchnumber());
        }
        //取最大序号
        umbrellaHardwareProductSerialnumberExample.setOrderByClause(" serialnumber desc ");
        UmbrellaHardwareProductSerialnumber maxProductSerialnumber = umbrellaHardwareProductSerialnumberService.selectFirstByExample(umbrellaHardwareProductSerialnumberExample);
        int remain = 999;
        int current = 1;
        if(maxProductSerialnumber != null ){
            current = Integer.parseInt(maxProductSerialnumber.getSerialnumber());
            remain = remain - current;
        }else{
            current = 0;
        }


        if(remain < batchCount){
            return new UmbrellaResult(UmbrellaResultConstant.FAILED, "此批次剩余数量已不足以分配,仅剩"+ remain );
        }
        for(int i=0;i<batchCount;i++) {
            UmbrellaHardwareProductSerialnumber newProductSerialnumber = new UmbrellaHardwareProductSerialnumber();
            BeanUtils.copyProperties(umbrellaHardwareProductSerialnumber, newProductSerialnumber);
            newProductSerialnumber.setSerialnumber(StringUtils.leftPad(String.valueOf(++ current ),3,"0"));
            StringBuffer buffer = new StringBuffer();
            buffer.append(newProductSerialnumber.getYears()).append(newProductSerialnumber.getClasscode())
                    .append(StringUtils.leftPad(newProductSerialnumber.getVersion(),2,"0")).append(newProductSerialnumber.getSubversion())
                    .append(newProductSerialnumber.getMonths()).append(newProductSerialnumber.getBatchnumber())
                    .append(newProductSerialnumber.getSerialnumber());
            newProductSerialnumber.setMachineCode(buffer.toString().toLowerCase());
            newProductSerialnumber.setStatus(Constant.CONST_NO);
            int count = umbrellaHardwareProductSerialnumberService.insert(newProductSerialnumber);
        }
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, batchCount);
    }

    @ApiOperation(value = "新增出厂编号(自定义)")
    @RequiresPermissions("umbrella:serialnum:create")
    @RequestMapping(value = "/createcustom", method = {RequestMethod.GET})
    public String createcustom(ModelMap modelMap) {
        //状态
        List<UpmsDict> statusList = upmsApiService.selectUpmsDictByCode(Constant.UPMS_YES_NO);
        modelMap.put("statusList", statusList);
        //代号
        List<UpmsDict> machineTypeList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_MACHINE_TYPE);
        modelMap.put("machineTypeList", machineTypeList);
        //产品分类代号
        List<UpmsDict> classCodeList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_CLASSCODE);
        modelMap.put("classCodeList", classCodeList);
        //换代或变型代号
        List<UpmsDict> subVersionList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_MSUBVERSION);
        modelMap.put("subVersionList", subVersionList);
        //使用去向
        List<UpmsDict> directionList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_MDIRECTION);
        modelMap.put("directionList", directionList);

        Date nowDate = new Date();

        UmbrellaHardwareProductSerialnumber umbrellaHardwareProductSerialnumber = new UmbrellaHardwareProductSerialnumber();
        umbrellaHardwareProductSerialnumber.setYears(DateUtil.date2Str(nowDate,DateUtil.FORMAT_YY));
        umbrellaHardwareProductSerialnumber.setMonths(DateUtil.date2Str(nowDate,DateUtil.FORMAT_MM));

        modelMap.put("umbrellaHardwareProductSerialnumber",umbrellaHardwareProductSerialnumber);
        return "/manage/serialnum/createcustom.jsp";
    }

    @ApiOperation(value = "新增出厂编号(自定义)")
    @RequiresPermissions("umbrella:serialnum:create")
    @ResponseBody
    @RequestMapping(value = "/createcustom", method = {RequestMethod.POST})
    public Object createcustom(@RequestParam(required = true, value = "batchCount") Integer batchCount,
                               @RequestParam(required = true, value = "prefixCode") String prefixCode,
                               UmbrellaHardwareProductSerialnumber umbrellaHardwareProductSerialnumber) {
        String userName = getLoginUserName();
        Date nowDate = new Date();
        umbrellaHardwareProductSerialnumber.setYears(StringUtils.leftPad(umbrellaHardwareProductSerialnumber.getYears(),2,"0"));
        umbrellaHardwareProductSerialnumber.setSubversion(StringUtils.leftPad(umbrellaHardwareProductSerialnumber.getSubversion(),2,"0"));
        umbrellaHardwareProductSerialnumber.setMonths(StringUtils.leftPad(umbrellaHardwareProductSerialnumber.getMonths(),2,"0"));
        umbrellaHardwareProductSerialnumber.setBatchnumber(StringUtils.leftPad(umbrellaHardwareProductSerialnumber.getBatchnumber(),3,"0"));

        umbrellaHardwareProductSerialnumber.setCreateuser(userName);
        umbrellaHardwareProductSerialnumber.setCreatetime(nowDate);
        umbrellaHardwareProductSerialnumber.setUpdateuser(userName);
        umbrellaHardwareProductSerialnumber.setUpdatetime(nowDate);

        //查询是否存在此批号及计算此批号最大剩余数量
        UmbrellaHardwareProductSerialnumberExample umbrellaHardwareProductSerialnumberExample = new UmbrellaHardwareProductSerialnumberExample();
        UmbrellaHardwareProductSerialnumberExample.Criteria criteria = umbrellaHardwareProductSerialnumberExample.createCriteria();
        criteria.andMachineCodeLike(prefixCode + "%");
        //取最大序号
        umbrellaHardwareProductSerialnumberExample.setOrderByClause(" machine_code desc ");
        UmbrellaHardwareProductSerialnumber maxProductSerialnumber = umbrellaHardwareProductSerialnumberService.selectFirstByExample(umbrellaHardwareProductSerialnumberExample);
        int remain = 999;
        int current = 1;
        if(maxProductSerialnumber != null ){
            current = Integer.parseInt(maxProductSerialnumber.getMachineCode().replace(prefixCode,""));
            remain = remain - current;
        }else{
            current = 0;
        }


        if(remain < batchCount){
            return new UmbrellaResult(UmbrellaResultConstant.FAILED, "剩余数量已不足以分配,仅剩"+ remain );
        }
        for(int i=0;i<batchCount;i++) {
            UmbrellaHardwareProductSerialnumber newProductSerialnumber = new UmbrellaHardwareProductSerialnumber();
            BeanUtils.copyProperties(umbrellaHardwareProductSerialnumber, newProductSerialnumber);
            newProductSerialnumber.setSerialnumber(StringUtils.leftPad(String.valueOf(++ current ),3,"0"));
            StringBuffer buffer = new StringBuffer();
            buffer.append(prefixCode)
                    .append(newProductSerialnumber.getSerialnumber());
            newProductSerialnumber.setMachineCode(buffer.toString().toLowerCase());
            newProductSerialnumber.setStatus(Constant.CONST_NO);
            int count = umbrellaHardwareProductSerialnumberService.insert(newProductSerialnumber);
        }
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, batchCount);
    }

    @ApiOperation(value = "出货更新")
    @RequiresPermissions("umbrella:serialnum:deliver")
    @RequestMapping(value = "/deliver/{ids}", method = {RequestMethod.GET})
    public String deliver(@PathVariable("ids") String ids, ModelMap modelMap) {

        modelMap.put("ids", ids);
        //状态
        List<UpmsDict> statusList = upmsApiService.selectUpmsDictByCode(Constant.UPMS_YES_NO);
        modelMap.put("statusList", statusList);
        //代号
        List<UpmsDict> machineTypeList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_MACHINE_TYPE);
        modelMap.put("machineTypeList", machineTypeList);
        //产品分类代号
        List<UpmsDict> classCodeList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_CLASSCODE);
        modelMap.put("classCodeList", classCodeList);
        //换代或变型代号
        List<UpmsDict> subVersionList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_MSUBVERSION);
        modelMap.put("subVersionList", subVersionList);
        //使用去向
        List<UpmsDict> directionList = upmsApiService.selectUpmsDictByCode(UmbrellaConstant.UM_UMBRELLA_MDIRECTION);
        modelMap.put("directionList", directionList);

        String[] idArray = ids.split("-");
        UmbrellaHardwareProductSerialnumber umbrellaHardwareProductSerialnumber = new UmbrellaHardwareProductSerialnumber();
        if(idArray.length ==1) {
            umbrellaHardwareProductSerialnumber = umbrellaHardwareProductSerialnumberService.selectByPrimaryKey(Integer.valueOf(idArray[0]));
        }

        Date nowDate = new Date();
        if(umbrellaHardwareProductSerialnumber.getWarrantystarttime()==null) {
            umbrellaHardwareProductSerialnumber.setWarrantystarttime(nowDate.getTime());
        }
        if(umbrellaHardwareProductSerialnumber.getWarrantyendtime()==null) {
            umbrellaHardwareProductSerialnumber.setWarrantyendtime(DateUtil.nextYear(nowDate).getTime());
        }
        modelMap.put("umbrellaHardwareProductSerialnumber",umbrellaHardwareProductSerialnumber);
        return "/manage/serialnum/deliver.jsp";
    }

    @ApiOperation(value = "出货更新")
    @RequiresPermissions("umbrella:serialnum:deliver")
    @RequestMapping(value = "/deliver/{ids}", method = {RequestMethod.POST})
    @ResponseBody
    public Object deliver(@PathVariable("ids") String ids, UmbrellaHardwareProductSerialnumber umbrellaHardwareProductSerialnumber) {
        String[] idArray = ids.split("-");
        UmbrellaHardwareProductSerialnumberExample umbrellaHardwareProductSerialnumberExample = new UmbrellaHardwareProductSerialnumberExample();
        UmbrellaHardwareProductSerialnumberExample.Criteria criteria = umbrellaHardwareProductSerialnumberExample.createCriteria();
        List<Integer> idList = new ArrayList<Integer>();
        for (String idStr : idArray) {
            idList.add(Integer.valueOf(idStr));
        }
        criteria.andIdIn(idList);
        int count = umbrellaHardwareProductSerialnumberService.updateByExampleSelective(umbrellaHardwareProductSerialnumber,umbrellaHardwareProductSerialnumberExample);
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, count);
    }

    @ApiOperation(value = "同步到自有平台")
    @RequiresPermissions("umbrella:serialnum:synownplatform")
    @RequestMapping(value = "/synownplatform/{ids}",method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object synownplatform(@PathVariable("ids") String ids) {
        if(StringUtils.isBlank(ids)){
            return new UmbrellaResult(UmbrellaResultConstant.FAILED, "未选择数据");
        }
        String[] idArr = ids.split(",");
        ArrayList<Integer> idList = new ArrayList<Integer>();
        for(int i=0;i<idArr.length;i++){
            idList.add(Integer.parseInt(idArr[i]));
        }
        int synCount = 0;
        UmbrellaHardwareProductSerialnumberExample productExample = new UmbrellaHardwareProductSerialnumberExample();
        productExample.createCriteria().andIdIn(idList);
        List<UmbrellaHardwareProductSerialnumber> productList = umbrellaHardwareProductSerialnumberService.selectByExample(productExample);

        if(productList==null || productList.isEmpty()){
            return new UmbrellaResult(UmbrellaResultConstant.FAILED, "未选择伞机");
        }
        //校验信息
        for(int i=0;i<productList.size();i++){
            if( productList.get(i).getDirection() ==null ){
                return new UmbrellaResult(UmbrellaResultConstant.FAILED,"使用去向未更新");
            }
            if( productList.get(i).getAgentid() ==null ){
                return new UmbrellaResult(UmbrellaResultConstant.FAILED,"代理信息未更新");
            }
            if(productList.get(i).getStatus() == Constant.CONST_YES ){
                return new UmbrellaResult(UmbrellaResultConstant.FAILED,"已同步数据不能再同步");
            }
        }
        for(int i=0;i<productList.size();i++) {
            UmbrellaHardwareProductSerialnumber productSerialnumber = productList.get(i);
            UmbrellaHardwareMachine umbrellaHardwareMachine = new UmbrellaHardwareMachine();
            String userName = getLoginUserName();
            Date nowDate = DateUtil.getDate();
            umbrellaHardwareMachine.setAgentId(productSerialnumber.getAgentid());
            umbrellaHardwareMachine.setCtime(nowDate.getTime());
            umbrellaHardwareMachine.setCuser(userName);
            umbrellaHardwareMachine.setCode(productSerialnumber.getMachineCode());
            umbrellaHardwareMachine.setType(Integer.parseInt(productSerialnumber.getVersion()));
            umbrellaHardwareMachine.setName("未定义");
            umbrellaHardwareMachine.setAddress("未定义");
            umbrellaHardwareMachine.setCity("未定义");
            umbrellaHardwareMachine.setCountry("中国");
            umbrellaHardwareMachine.setLaunchtime(nowDate.getTime());
            umbrellaHardwareMachine.setLasttimeReport(nowDate);

            umbrellaHardwareMachine.setPathcount(productSerialnumber.getPathcount());
            umbrellaHardwareMachine.setCapacity(40);
            int count = umbrellaHardwareMachineService.insertSelective(umbrellaHardwareMachine);
            //同步增加伞道
            if (count > 0) {
                synCount = synCount + 1;
                //更新同步标识
                UmbrellaHardwareProductSerialnumber updateProductSerialnumber = new UmbrellaHardwareProductSerialnumber();
                updateProductSerialnumber.setStatus(Constant.CONST_YES);
                updateProductSerialnumber.setId(productSerialnumber.getId());
                umbrellaHardwareProductSerialnumberService.updateByPrimaryKeySelective(updateProductSerialnumber);
                //插入分机伞道
                for (int j = 0; j < umbrellaHardwareMachine.getPathcount(); j++) {
                    UmbrellaHardwarePath umbrellaHardwarePath = new UmbrellaHardwarePath();
                    int pathNumber = j + 1;
                    umbrellaHardwarePath.setCode(umbrellaHardwareMachine.getCode() + "0" + pathNumber);
                    umbrellaHardwarePath.setNumber(pathNumber);
                    umbrellaHardwarePath.setName(umbrellaHardwareMachine.getCode() + "0" + pathNumber);
                    umbrellaHardwarePath.setMachineCode(umbrellaHardwareMachine.getCode());
                    umbrellaHardwarePath.setAgentId(productSerialnumber.getAgentid());
                    umbrellaHardwarePath.setStatus(Constant.CONST_ENABLE);
                    umbrellaHardwarePath.setFlag(Constant.CONST_FLAG_NORMAL);
                    umbrellaHardwarePath.setRemainder(0);
                    umbrellaHardwarePath.setCapacity(umbrellaHardwareMachine.getPathcount() == 1 ? umbrellaHardwareMachine.getCapacity() : 0);
                    umbrellaHardwarePath.setCuser(userName);
                    umbrellaHardwarePath.setCtime(nowDate.getTime());
                    umbrellaHardwarePathService.insert(umbrellaHardwarePath);
                }
            }
        }
        return new UmbrellaResult(UmbrellaResultConstant.SUCCESS, synCount);
    }
}
